﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ProjectPacker
{
    public class PathWorker
    {
        public static void PackPathCreate(List<String> items)
        {
            foreach (String item in items)
            {
                String[] Pathes = item.Split(new Char[] { ':' }, 3);
                String tempPath = MyConfig.TempFolder + Pathes[1];
                String DirPath;
                if (Pathes[0] == "1")
                {
                    DirPath = Path.GetDirectoryName(tempPath);
                }
                else
                {
                    DirPath = tempPath;
                }
                if (!Directory.Exists(DirPath))
                {
                    Directory.CreateDirectory(DirPath);
                }
                if (Pathes[0] == "1")
                {
                    File.Copy(Pathes[2], tempPath, true);
                }
                else
                {
                    CopyDirectory(Pathes[2], tempPath);
                }
                MessageSender.Progress_Done++;
            }
        }
        //文件夹复制函数编写
        public static void CopyDirectory(String source, String destination)
        {
            DirectoryInfo info = new DirectoryInfo(source);
            foreach (FileSystemInfo fsi in info.GetFileSystemInfos())
            {
                //目标路径destName = 目标文件夹路径 + 原文件夹下的子文件(或文件夹)名字
                //Path.Combine(string a ,string b) 为合并两个字符串
                String destName = Path.Combine(destination, fsi.Name);
                //如果是文件类,就复制文件
                if (fsi is System.IO.FileInfo)
                    File.Copy(fsi.FullName, destName);
                //如果不是 则为文件夹,继续调用文件夹复制函数,递归
                else
                {
                    Directory.CreateDirectory(destName);
                    CopyDirectory(fsi.FullName, destName);
                }
            }
        }
    }
}
